package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.HouseMapper;
import com.yx.dao.OwnerMapper;
import com.yx.dao.PropertyTypeMapper;
import com.yx.dao.RecordsMapper;
import com.yx.model.*;
import com.yx.service.IRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class RecordsServiceImpl extends ServiceImpl<RecordsMapper, Records> implements IRecordsService {

    @Autowired
    private RecordsMapper recordsDao;

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private PropertyTypeMapper propertyTypeMapper;


    @Override
    public IPage<RecordVo> findRecordsAll(Page<Records> recordVoPage, RecordVo recordVo) {
        Integer typeId = recordVo.getTypeId();

        QueryWrapper<Records> wrapper = new QueryWrapper<>();
        // 封装条件
        if (!StringUtils.isEmpty(typeId)) {
            wrapper.eq("type_id", typeId);
        }
        Page<Records> recordsPage = recordsDao.selectPage(recordVoPage, wrapper);

        // 进行数据填充
        Page<RecordVo> voPage = new Page<>();
        voPage.setTotal(recordsPage.getTotal());

        List<Records> record = recordsPage.getRecords();
        List<RecordVo> records = record.stream().map(records1 -> {
            RecordVo recordVo1 = new RecordVo();
            BeanUtils.copyProperties(records1, recordVo1);
            return recordVo1;
        }).collect(Collectors.toList());

        records = records.stream().map(recordVo1 -> {

            Integer houseId = recordVo1.getHouseId();
            // 查询户主的信息
            QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("house_id", houseId);
            Owner owner = ownerMapper.selectOne(queryWrapper);
            recordVo1.setUsername(owner.getUsername());

            // 查询门牌号
            Integer houseId1 = recordVo1.getHouseId();
            House house = houseMapper.selectById(houseId1);
            recordVo1.setNumbers(house.getNumbers());

            // 查询费用类型
            Integer typeId1 = recordVo1.getTypeId();
            PropertyType propertyType = propertyTypeMapper.selectById(typeId1);
            recordVo1.setTypename(propertyType.getName());

            // 返回数据
            return recordVo1;
        }).collect(Collectors.toList());

        // 填充数据
        voPage.setRecords(records);

        // 筛选数据
        String numbers = recordVo.getNumbers();
        if (!StringUtils.isEmpty(numbers)) {
            records = records.stream().filter(recordVo1 -> {
                String numbers1 = recordVo1.getNumbers();
                if (numbers1.equals(numbers)) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());
        }

        // 填充数据
        voPage.setRecords(records);

        return voPage;
    }

    @Override
    public PageInfo<RecordVo> findRecordsAll(int page, int limit, RecordVo recordVo) {
        PageHelper.startPage(page, limit);
        List<RecordVo> list = recordsDao.queryRecordsAll(recordVo);
        return new PageInfo<>(list);
    }

    @Override
    public Records queryByHouIdAndTypeId(Integer houId, Integer typeId) {
        return recordsDao.queryByHouIdAndTypeId(houId, typeId);
    }

    @Override
    public int add(Records building) {
        return baseMapper.insert(building);
    }


    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Records building) {
        return baseMapper.updateById(building);
    }

    @Override
    public Records findById(Long id) {
        return baseMapper.selectById(id);
    }


}
